<template>
  <div class="password-container">
    <div class="password-card">
      <h2 class="card-title">
        <i class="el-icon-lock"></i>
        修改密码
      </h2>

      <el-form 
        ref="passwordForm" 
        :model="form" 
        :rules="rules"
        label-width="120px"
        class="password-form"
      >
        <el-form-item label="当前密码" prop="oldPassword">
          <el-input 
            v-model="form.oldPassword" 
            type="password"
            placeholder="请输入当前密码"
            show-password
          ></el-input>
        </el-form-item>

        <el-form-item label="新密码" prop="newPassword">
          <el-input 
            v-model="form.newPassword" 
            type="password"
            placeholder="请输入新密码"
            show-password
          ></el-input>
          <div class="password-strength" v-if="form.newPassword">
            <span>密码强度:</span>
            <div class="strength-meter">
              <div 
                class="strength-bar" 
                :style="{ width: passwordStrength + '%' }"
                :class="strengthClass"
              ></div>
            </div>
            <span class="strength-text">{{ strengthText }}</span>
          </div>
        </el-form-item>

        <el-form-item label="确认新密码" prop="confirmPassword">
          <el-input 
            v-model="form.confirmPassword" 
            type="password"
            placeholder="请再次输入新密码"
            show-password
          ></el-input>
        </el-form-item>

        <el-form-item class="form-actions">
          <el-button 
            type="primary" 
            @click="submitForm"
            :loading="submitting"
          >
            确认修改
          </el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 密码修改提示 -->
      <div class="password-tips">
        <h4><i class="el-icon-info"></i> 密码要求：</h4>
        <ul>
          <li>长度至少8个字符</li>
          <li>包含大小写字母</li>
          <li>包含数字</li>
          <li>包含特殊字符</li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ModifyPassword',
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.form.confirmPassword !== '') {
          this.$refs.passwordForm.validateField('confirmPassword')
        }
        callback()
      }
    }
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.form.newPassword) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      form: {
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      },
      rules: {
        oldPassword: [
          { required: true, message: '请输入当前密码', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, validator: validatePass, trigger: 'blur' },
          { min: 8, message: '密码长度至少8个字符', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, validator: validatePass2, trigger: 'blur' }
        ]
      },
      submitting: false
    }
  },
  computed: {
    passwordStrength() {
      const password = this.form.newPassword
      if (!password) return 0
      
      let strength = 0
      // 检查长度
      if (password.length >= 8) strength += 25
      // 检查是否包含数字
      if (/\d/.test(password)) strength += 25
      // 检查是否包含小写字母
      if (/[a-z]/.test(password)) strength += 25
      // 检查是否包含大写字母
      if (/[A-Z]/.test(password)) strength += 25
      
      return strength
    },
    strengthClass() {
      const strength = this.passwordStrength
      if (strength <= 25) return 'weak'
      if (strength <= 50) return 'medium'
      if (strength <= 75) return 'strong'
      return 'very-strong'
    },
    strengthText() {
      const strength = this.passwordStrength
      if (strength <= 25) return '弱'
      if (strength <= 50) return '中'
      if (strength <= 75) return '强'
      return '非常强'
    }
  },
  methods: {
    submitForm() {
      this.$refs.passwordForm.validate(valid => {
        if (valid) {
          this.submitting = true
          this.$http.post("/updateUserPw", {
            oldUPw: this.form.oldPassword,
            newUPw: this.form.newPassword
          }).then(() => {
            this.$message.success('密码修改成功')
            this.resetForm()
            this.submitting = false
          }).catch(error => {
            this.$message.error('密码修改失败')
            console.error(error)
            this.submitting = false
          })
        }
      })
    },
    resetForm() {
      this.$refs.passwordForm.resetFields()
    }
  }
}
</script>

<style scoped>
.password-container {
  padding: 24px;
  background-color: #f0f2f5;
  min-height: calc(100vh - 48px);
}

.password-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  padding: 32px;
  max-width: 600px;
  margin: 0 auto;
}

.card-title {
  display: flex;
  align-items: center;
  margin: 0 0 32px;
  color: #303133;
  font-size: 24px;
}

.card-title i {
  margin-right: 8px;
  color: #409EFF;
}

.password-form {
  margin-bottom: 32px;
}

.password-strength {
  margin-top: 8px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: #909399;
}

.strength-meter {
  flex: 1;
  height: 4px;
  background: #ebeef5;
  border-radius: 2px;
  overflow: hidden;
}

.strength-bar {
  height: 100%;
  transition: all 0.3s ease;
}

.strength-bar.weak { background-color: #f56c6c; }
.strength-bar.medium { background-color: #e6a23c; }
.strength-bar.strong { background-color: #67c23a; }
.strength-bar.very-strong { background-color: #409EFF; }

.form-actions {
  margin-top: 32px;
  text-align: center;
}

.password-tips {
  padding: 16px;
  background: #f8f9fa;
  border-radius: 4px;
}

.password-tips h4 {
  display: flex;
  align-items: center;
  margin: 0 0 8px;
  color: #606266;
}

.password-tips i {
  margin-right: 4px;
  color: #409EFF;
}

.password-tips ul {
  margin: 0;
  padding-left: 20px;
  color: #909399;
}

.password-tips li {
  margin: 4px 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .password-card {
    padding: 20px;
  }
}
</style>
